package model

import (
	"fmt"
	"ginblog/config"
	_ "github.com/go-sql-driver/mysql"
	"github.com/jinzhu/gorm"
	"time"
)

var db *gorm.DB

func InitDb() (err error) {
	db, err = gorm.Open(config.Type, fmt.Sprintf("%s:%s@tcp(%s:%s)/%s?charset=utf8mb4&parseTime=True",
		config.User,
		config.Password,
		config.Host,
		config.Port,
		config.Db))
	if err != nil {
		return
	}
	//db.AutoMigrate(&User{}, &Article{}, &Category{})
	db.SingularTable(true)
	db.DB().SetMaxIdleConns(10)
	db.DB().SetMaxOpenConns(100)
	//db.DB().SetConnMaxIdleTime(1 * time.Second)
	db.DB().SetConnMaxLifetime(5*time.Second)
	//db.Close()
	return
}
func CloseDB()  {
	fmt.Println("數據庫鏈接釋放")
}